﻿using System;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Web.Mvc;

namespace BabyShop.Site.Exts.Attributes
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class UserHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext) {
            if (filterContext.Exception is DbEntityValidationException) {
                var dbEx = filterContext.Exception as DbEntityValidationException;
                foreach (var validationErrors in dbEx.EntityValidationErrors) {
                    foreach (var validationError in validationErrors.ValidationErrors) {
                        Debug.WriteLine("Property: {0} Error: {1}",
                                               validationError.PropertyName,
                                               validationError.ErrorMessage);
                    }
                }
            }
            base.OnException(filterContext);
        }
    }
}